import 'dart:ui' as ui;
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class SquareViewPainter extends CustomPainter {
  Paint _paint = new Paint();
  BuildContext context;
  List listScale;
  Color color;
  double maxLength = 230.0;
  double rectHeight = 40.0;
  double textMargin = 10.0;
  double margingLeft = 15.0;

  SquareViewPainter(this.context, this.listScale);

  @override
  void paint(Canvas canvas, Size size) {
    String drwaText = '';
    for (var index = 0; index < listScale.length; index++) {
      if (index == 0) {
        drwaText = '进件成功商户 ${listScale[index]}';
        _paint.color = Color(int.parse("0xffE22B2B"));
      } else if (index == 1) {
        drwaText = '历史有交易  ${listScale[index]}';
        _paint.color = Color(int.parse("0xffFF543E"));
      } else if (index == 2) {
        drwaText = '近30日有交易  ${listScale[index]}';
        _paint.color = Color(int.parse("0xffF7760E"));
      } else if (index == 3) {
        drwaText = '近7日有交易  ${listScale[index]}';
        _paint.color = Color(int.parse("0xffF8B90D"));
      } else if (index == 4) {
        drwaText = '今日有交易  ${listScale[index]}';
        _paint.color = Color(int.parse("0xffFFD86C"));
      }

      if (listScale[0] != 0) {
        margingLeft = maxLength * listScale[index] / listScale[0] + textMargin;
      }

      Path path = new Path();
      if (listScale[index] > 0) {
        path.moveTo(0, rectHeight * index);
        path.lineTo(
            maxLength * listScale[index] / listScale[0], rectHeight * index);
        if (index == listScale.length - 1) {
          path.lineTo(maxLength * listScale[index] / listScale[0],
              rectHeight * (index + 1));
        } else {
          path.lineTo(maxLength * listScale[index + 1] / listScale[0],
              rectHeight * (index + 1));
        }
        path.lineTo(0, rectHeight * (index + 1));
        canvas.drawPath(path, _paint);

        ParagraphBuilder pb = ParagraphBuilder(ParagraphStyle(
          fontSize: 12,
        ))
          ..pushStyle(ui.TextStyle(color: Color(int.parse("0xff666666"))))
          ..addText(drwaText);
        ParagraphConstraints pc = ParagraphConstraints(width: 100);
        Paragraph paragraph = pb.build()..layout(pc);
        canvas.drawParagraph(paragraph,
            Offset(margingLeft, rectHeight * index + rectHeight / 4));
      }
    }
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}
